package com.example.extra.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.extra.common.R;
import com.example.extra.entity.ProductPdf;
import com.example.extra.service.IProductPdfService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品PDF管理接口
 * @author system
 */
@Slf4j
@Tag(name = "产品PDF管理接口")
@RestController
@RequestMapping("/api/product-pdf")
@RequiredArgsConstructor
public class ProductPdfController {
    
    private final IProductPdfService productPdfService;

    @Value("${file.prefix}")
    private String prefix;

    @PostMapping("/upload")
    @Operation(summary = "上传产品PDF文件到MinIO")
    public R<String> uploadPdf(
        @Parameter(description = "PDF文件", required = true)
        @RequestParam("file") MultipartFile file,

        @Parameter(description = "创建用户ID", required = true)
        @RequestParam("createUserId") String createUserId,

        @Parameter(description = "PDF文件报告类型，1-日报,2-要报,3-报告", required = true)
        @RequestParam("storageType") Integer storageType,

        @Parameter(description = "目录，可以重新自己定义目录，也可以复用minio桶里的目录，以/线结尾", required = true)
        @RequestParam("catalogue") String catalogue) {
        try {
            if (file.isEmpty()) {
                return R.fail("文件不能为空");
            }
            
            if (createUserId == null || createUserId.trim().isEmpty()) {
                return R.fail("创建用户ID不能为空");
            }
            
            String fileUrl = productPdfService.uploadPdf(file, createUserId,catalogue,storageType);
            return R.ok("PDF文件上传成功", fileUrl);
            
        } catch (Exception e) {
            log.error("上传PDF文件失败", e);
            return R.fail("上传PDF文件失败: " + e.getMessage());
        }
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询PDF文件")
    @Parameter(name = "page", description = "页码", required = true)
    @Parameter(name = "size", description = "每页大小", required = true)
    @Parameter(name = "createUserId", description = "创建用户ID（可选）")
    @Parameter(name = "storageType", description = "PDF文件报告类型(1-日报,2-要报,3-报告)（可选）")
    public R<IPage<ProductPdf>> getPdfPage(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size,
            @RequestParam(value = "createUserId", required = false) String createUserId,
            @RequestParam(value = "storageType", required = false) Integer storageType) {
        try {
            if (page == null || page < 1) {
                return R.fail("页码必须大于0");
            }
            
            if (size == null || size < 1) {
                return R.fail("每页大小必须大于0");
            }
            
            IPage<ProductPdf> result = productPdfService.getPdfPage(page, size, createUserId,storageType);
            result.getRecords().forEach(productPdf -> {
                productPdf.setPdfPath(productPdf.getPdfPath());
            });
            return R.ok("查询成功", result);
            
        } catch (Exception e) {
            log.error("分页查询PDF文件失败", e);
            return R.fail("查询失败: " + e.getMessage());
        }
    }
}
